<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ReplyTap;
use App\Admin\Repositories\WxFeedback;
use App\Wen\Utils\Utils;
use App\Models\WxFeedback as Model;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxFeedbackController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected function grid()
    {
        return Grid::make(new WxFeedback(['user']), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'feedback_content','user_id','user.user_name', 'circle_name', 'feedback_reply'])->placeholder('搜索ID，内容，用户ID，用户名字，圈子名字...')->width(50);
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user.user_avatar', '用户头像')->image('', 50);
            $grid->column('user.user_name', '用户名');
            $grid->column('feedback_type');
            $grid->column('feedback_content')->limit(30);
            $grid->column('feedback_imgs')->display(function ($v) {
                if(_empty_($v)){
                    return '';
                }
                $imgs = explode(',', $v);
                $html = '<div style="max-width: 350px; overflow-x: scroll;">';
                foreach ($imgs as $img){
                    $html .= '<img data-action="preview-img" style="max-width: 50px;margin-right: 10px;" src="'.Utils::imgWithStyle($img).'" / >';
                }
                return $html.'</div>';
            })->width('50px');
            $grid->column('feedback_contact');
            $grid->column('feedback_reply')->limit(6);
            $grid->column('feedback_state')->using([0 => '未受理', 1 => '已受理'])->label([
                0 => 'red',
                1 => 'primary',
            ]);
//            $grid->column('created_at');
            $grid->column('is_solve')->using([0 => '未反馈', 1 => '已解决', 2 => '未解决']);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new ReplyTap())->setKey($this->id));
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
            });
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxFeedback(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('feedback_type');
            $show->field('feedback_content');
            $show->field('feedback_reply');
            $show->field('feedback_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxFeedback(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('feedback_type');
            $form->text('feedback_content');
            $form->text('feedback_reply');
            $form->text('feedback_state');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id ,$feedback_reply
     *
     * @return Show
     */
    protected function reply($id, $feedback_reply)
    {
        $model = new Model();
        $feedback = $model->where('id', $id)->first();
        $feedback->feedback_reply = $feedback_reply;
        return $feedback->save();
    }
}
